<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      思路：https://leetcode-cn.com/problems/search-in-rotated-sorted-array/solution/pythonjs-er-fen-fa-33-sou-suo-xuan-zhuan-pai-xu-sh/
     */
    let nums = [4,5,6,7,0,1,2], target = 0;
    // let nums = [4,5,6,7,0,1,2], target = 3;

    function search(nums, target) {
      let start = 0;
      let end = nums.length - 1;

      while (start <= end) {
        let mid = start + end >> 1;
        if (target === nums[mid]) return mid;

        if (nums[mid] >= nums[start]) { // ️⚠️注意这里的等号
          if (target >= nums[start] && target < nums[mid])
            end = mid - 1;
          else 
            start = mid + 1;
        }
        else {
          if (target > nums[mid] && target <= nums[end]) 
            start = mid + 1;
          else 
            end = mid - 1;
        }
      }
      return -1;
    }
    console.log(search(nums, target));
  </script>
</body>
</html>